<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    private $statusCode;
    private $code;
    private $msg;
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
        \App\Exceptions\BaseException::class,// 不记录自定义异常
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Exception $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        //异常不会走到这里,被dingo接管了,可以在 \App\Providers\ErrorServiceProvider 中接管错误处理，这里不用看
        //非dingo部分会走这里
        
        if ($exception instanceof BaseException) { // 自定义异常处理类,不记录
            $this->code = $exception->getCode();
            $this->msg = $exception->getMessage();
            $this->statusCode = 200;
        } else { // Laravel内部错误
            $this->statusCode = 500;
            $this->code = $exception->getCode() ?: 500;
            $this->msg = '内部错误,工程师正在维修...';
            $this->recordErrorLog($exception);
        }
        //获取实例请求
        $result = [
            'status_code' => $this->statusCode,
            'code' => $this->code,
            'message' => $this->msg,
            'request_url' => $request = $request->url()
        ];
        return response()->json($result, $this->statusCode);
    }

    /**
     * 将异常写入日志
     * @param Exception $e
     */
    private function recordErrorLog(Exception $e)
    {
        Log::error($e->getMessage());
    }

    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Illuminate\Auth\AuthenticationException $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        return redirect()->guest('login');
    }

    protected function invalidJson($request, ValidationException $exception)
    {
        return parent::invalidJson($request, $exception); // TODO: Change the autogenerated stub
    }
}
